package site.teamo.biu.net.server.configuration;

import com.alibaba.fastjson2.JSON;
import jakarta.annotation.PostConstruct;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import site.teamo.biu.net.common.web.entity.SystemConfig;
import site.teamo.biu.net.common.web.mapper.SystemConfigMapper;
import site.teamo.biu.net.server.core.http_https_proxy.TrafficStatistics;
import site.teamo.biu.tool.spring.listener.ApplicationReadyListener;

/**
 * @author haocongshun
 * @date 2023/06/08 17:15:50
 */
@Configuration
public class ServerConfiguration {
    public static final SystemConfig SERVER_NAME = new SystemConfig("server-name", "服务名称", true, () -> "代理服务器-" + System.currentTimeMillis());
    public static final SystemConfig HTTP_HTTPS_PROXY_SERVICE_LISTEN_PORT = new SystemConfig("http-https-proxy-service-listen-port", "代理服务监听端口", true, () -> "19091");
    public static final SystemConfig HTTP_HTTPS_PROXY_SERVICE_TRAFFIC_STATISTICS = new SystemConfig("http-https-proxy-service-traffic-statistics", "代理服务流量统计", true, () -> JSON.toJSONString(new TrafficStatistics()));

    @Resource
    private SystemConfigMapper systemConfigMapper;

    @Autowired
    private ApplicationReadyListener listener;

    @PostConstruct
    public void init() {
        listener.addJob("服务端信息初始化", () -> {
            systemConfigMapper.insertNotExists(SERVER_NAME.toSystemConfigDO());
            systemConfigMapper.insertNotExists(HTTP_HTTPS_PROXY_SERVICE_LISTEN_PORT.toSystemConfigDO());
            systemConfigMapper.insertNotExists(HTTP_HTTPS_PROXY_SERVICE_TRAFFIC_STATISTICS.toSystemConfigDO());
        });
    }
}
